package com.tstudy.laoshibang.group;

import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.mode.TGroup;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
/**
 *  我加入的群组adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
public class GroupAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<TGroup> mItems;
	int lineHeight = CommonUtil.dip2px(1);
	boolean mIsAll;
	
	String viewCountStr = BaseApplication.getResString(R.string.active_list_view_count);
	String freeStr = BaseApplication.getResString(R.string.active_list_free);
	int redTextColor = BaseApplication.getResColor(R.color.theme_red);
	int grayTextColor = BaseApplication.getResColor(R.color.text_color_gray);
	public GroupAdapter(boolean isAll) {
		mIsAll = isAll;
		mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	public void setData(List<TGroup> items) {
		setData(items,true);
	}
	public void setData(List<TGroup> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<TGroup> getData() {
		return mItems;
	}
	

	@Override
	public Object getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if(convertView ==  null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.group_item, null);
			viewHolder.image = (ImageView)convertView.findViewById(R.id.group_item_image);
			viewHolder.name = (TextView) convertView.findViewById(R.id.group_item_name);
			viewHolder.description = (TextView) convertView.findViewById(R.id.group_item_desc);
			viewHolder.enterIcon = (ImageView) convertView.findViewById(R.id.group_item_add_icon);
			viewHolder.sp = (View) convertView.findViewById(R.id.group_item_line);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(viewHolder != null){
			setConvertView(viewHolder,position,convertView);
		}
		return convertView;
	}
	
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final TGroup group = (TGroup) getItem(position);
		viewHolder.name.setText(group.name);
		viewHolder.description.setText(group.description);
		RelativeLayout.LayoutParams spLp = (LayoutParams) viewHolder.sp.getLayoutParams();
		if (position == mItems.size()-1) {
			spLp.height = 1;
		}else{
			spLp.height = lineHeight;
		}
		if (mIsAll) {
			switch (group.status.key) {
			case CONSTANT.GROUP_APPLY_INIT:
				viewHolder.enterIcon.setImageResource(R.drawable.group_btn_join);
				viewHolder.enterIcon.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mActionCallBack != null) {
							mActionCallBack.onAddClick(group, position);
						}
					}
				});
				break;
			case CONSTANT.GROUP_APPLY_ING:
				viewHolder.enterIcon.setImageResource(R.drawable.group_btn_audit);
				viewHolder.enterIcon.setOnClickListener(null);
				break;
			case CONSTANT.GROUP_APPLY_FAILED:
				viewHolder.enterIcon.setImageResource(R.drawable.group_btn_join);
				viewHolder.enterIcon.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mActionCallBack != null) {
							mActionCallBack.onAddClick(group, position);
						}
					}
				});
				break;
			case CONSTANT.GROUP_APPLY_SUCCESS:
				viewHolder.enterIcon.setImageResource(R.drawable.group_btn_ined);
				viewHolder.enterIcon.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mActionCallBack != null) {
							mActionCallBack.onEnterClick(group, position);
						}
					}
				});
				break;
			default:
				break;
			}
		}else{
			viewHolder.enterIcon.setImageResource(R.drawable.group_btn_in);
			viewHolder.enterIcon.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mActionCallBack != null) {
						mActionCallBack.onEnterClick(group,position);
					}
				}
			});
		}
		HttpManager.getInstance().loadCommonImage(viewHolder.image,group.imgIdxName);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallBack != null) {
					mActionCallBack.onViewClick(group.groupId,position);
				}
			}
		});
	}
	
	static class ViewHolder{
		ImageView image;
		TextView name;
		TextView description;
		ImageView enterIcon;
		View sp;
	}
	
	public interface AdapterCallBack{
		public void onViewClick(String groupId,int position);
		public void onAddClick(TGroup group,int position);
		public void onEnterClick(TGroup group,int position);
	}
	AdapterCallBack mActionCallBack;
	public void setAdatperCallBack(AdapterCallBack actionCallBack){
		mActionCallBack = actionCallBack;
	}
}
